#include "mpu6050.h"

int main(int argc, char * argv[]){
	
	int fd = -1;
	unsigned short pdata = 0;
	int ret = 0;

	if(argc < 2){
		printf("arg is too few \n");
		//非0的返回值 表示失败
		return 1;
	}

	fd = open(argv[1], O_RDWR);
	if(fd < 0){
		printf("open mpu6050 failed \n");
		//非0的返回值 表示失败
		return 2;
	}

	ret = init_mpu6050(fd);
	if(ret < 0){
		printf("init mpu6050 failed \n");
		return 3;
	}

	//这种写法无法处理函数发生异常时的情况，
	//1.应当将返回值作为判断函数是否异常的标志，若需要拿到函数处理值，可以传入地址
	//2. 参数需要设置成unsigned类型 不然默认是当作有符号的情况处理
	//修改为后面的写法
	/*
	while(1){
		sleep(2);
		printf("ACCEL X is 0x%x\n", read_from_mpu6050(fd, ACCEL_XOUT_H)<<8 | read_from_mpu6050(fd, ACCEL_XOUT_L));
		printf("ACCEL Y is 0x%x\n", read_from_mpu6050(fd, ACCEL_YOUT_H)<<8 | read_from_mpu6050(fd, ACCEL_YOUT_L));
		printf("ACCEL Z is 0x%x\n", read_from_mpu6050(fd, ACCEL_ZOUT_H)<<8 | read_from_mpu6050(fd, ACCEL_ZOUT_L));
		
		printf("GYRO X is 0x%x\n", read_from_mpu6050(fd, GYRO_XOUT_H)<<8 | read_from_mpu6050(fd, GYRO_XOUT_L));
		printf("GYRO Y is 0x%x\n", read_from_mpu6050(fd, GYRO_YOUT_H)<<8 | read_from_mpu6050(fd, GYRO_YOUT_L));
		printf("GYRO Z is 0x%x\n", read_from_mpu6050(fd, GYRO_ZOUT_H)<<8 | read_from_mpu6050(fd, GYRO_ZOUT_L));
		
		printf("TEMP is 0x%x\n", read_from_mpu6050(fd, TEMP_OUT_H)<<8 | read_from_mpu6050(fd, TEMP_OUT_L));
		printf("\n");
	}*/


	while(1){
		sleep(2);
		printf("ACCEL X is 0x%x\n", read_data_from_mpu6050(fd, ACCEL_XOUT_H, ACCEL_XOUT_L, &pdata) == -1 ? 0 : pdata);
		printf("ACCEL Y is 0x%x\n", read_data_from_mpu6050(fd, ACCEL_YOUT_H, ACCEL_YOUT_L, &pdata) == -1 ? 0 : pdata);
		printf("ACCEL Z is 0x%x\n", read_data_from_mpu6050(fd, ACCEL_ZOUT_H, ACCEL_ZOUT_L, &pdata) == -1 ? 0 : pdata);
		
		printf("GYRO X is 0x%x\n", read_data_from_mpu6050(fd, GYRO_XOUT_H, GYRO_XOUT_L, &pdata) == -1 ? 0 : pdata);
		printf("GYRO Y is 0x%x\n", read_data_from_mpu6050(fd, GYRO_YOUT_H, GYRO_YOUT_L, &pdata) == -1 ? 0 : pdata);
		printf("GYRO Z is 0x%x\n", read_data_from_mpu6050(fd, GYRO_ZOUT_H, GYRO_ZOUT_L, &pdata) == -1 ? 0: pdata);
		
		printf("TEMP is 0x%x\n", read_data_from_mpu6050(fd, TEMP_OUT_H, TEMP_OUT_L, &pdata) == -1 ? 0 : pdata);
		printf("\n");
	}


	close(fd);
	fd = -1;

	return 0;
}
